// Problem 065: Convergents of e
//
// e=[2;1,2,1,1,4,1,1,6,1,...,1,2*k,1...]
// Find the sum of digits in the numerator of the 100th convergent of the continued fraction for e.

package main

import (
	"fmt"
	"math/big"
)

func p065() {
	result := big.NewRat(1, 1)
	var n int64
	for n = 99; n > 1; n-- {
		if n%3 == 0 {
			handle065(n*2/3, result)
		} else {
			handle065(1, result)
		}
	}
	handle065(2, result)
	sum := 0
	for _, digit := range result.Num().String() {
		sum += int(digit) - 48
	}
	fmt.Println("Problem 065:", sum)
}

func handle065(a int64, result *big.Rat) {
	result.Inv(result)
	m := big.NewInt(a)
	num := big.NewInt(1)
	num.Mul(result.Denom(), m)
	num.Add(num, result.Num())
	result.Num().SetString(num.String(), 10)
}
